package util

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
)

func Paginate(g *gin.Context) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		page, _ := strconv.Atoi(g.Query("page"))
		if page == 0 {
			page = 1
		}
		pageSize, _ := strconv.Atoi(g.Query("size"))
		switch {
		case pageSize > 1000:
			pageSize = 1000
		case pageSize <= 0:
			pageSize = 10
		}
		return db.Limit(pageSize).Offset((page - 1) * pageSize)
	}
}
